﻿Public Class frmMaterial

    Private mvarChanged As Boolean = False
    Private mvarCancel As Boolean = False
    Private mvarMaterial As clsInhMaterial
    Private mvarDbHolder As clsDataBase
    Private mvarWyznEdytowania As Boolean = False
    Private mvarBlokowanie As clsRecordLock
    Private mvarWyznBlokowania As Boolean = False
    Private mvarUzytkownik As clsUzytkownik
    Private mvarTextList As List(Of TextBox)

    Public Sub New(IdMaterial As Integer, IdGatunek As Integer)

        ' To wywołanie jest wymagane przez projektanta.
        InitializeComponent()

        ' Dodaj inicjację po wywołaniu funkcji InitializeComponent().
        mvarDbHolder = New clsDataBase(VAR_CONNECTION_PARAMETERS, VAR_ERROR_HANDLER)
        mvarUzytkownik = CType(clsUzytkownik.Instance(), clsUzytkownik)
        mvarTextList = New List(Of TextBox)
        PobierzWszystkichDostawcow()
        PobierzWszystkieGatunkiMaterialu()
        PobierzWszystkieWaluty()

        If IdMaterial = 0 Then
            mvarMaterial = New clsInhMaterial
            WyczyscInformacje()
            SzukajGatunku(IdGatunek)
            mvarChanged = False
        Else
            mvarMaterial = New clsInhMaterial(IdMaterial, mvarDbHolder)
            Blokowanie()
            Me.Text = "WIREMET - EDYCJA MATERIAŁU"
            UzupelnienieDanych()
            mvarWyznEdytowania = True
            mvarChanged = False
            cmbDostawca.Focus()
        End If

    End Sub

#Region "Pobierz"

    Private Sub PobierzWszystkichDostawcow()

        Dim lvarDataTable As New DataTable

        lvarDataTable = clsDostawca.PobierzWszystkieOpcje(mvarDbHolder, VAR_ERROR_HANDLER)
        cmbDostawca.Items.Clear()
        If Not lvarDataTable Is Nothing Then
            For Each lvarRow As DataRow In lvarDataTable.Rows
                cmbDostawca.Items.Add(New clsDostawca(CStr(lvarRow("id")), CStr(IIf(IsDBNull(lvarRow("nazwa_firmy")), "", lvarRow("nazwa_firmy")))))
            Next
        End If

    End Sub

    Private Sub PobierzWszystkieGatunkiMaterialu()

        Dim lvarDataTable As New DataTable

        lvarDataTable = clsGatunek.PobierzWszystkieGatunki(mvarDbHolder, VAR_ERROR_HANDLER)
        cmbGatunek.Items.Clear()
        If Not lvarDataTable Is Nothing Then
            For Each lvarRow As DataRow In lvarDataTable.Rows
                cmbGatunek.Items.Add(New clsGatunek(CInt(lvarRow("id")), CStr(IIf(IsDBNull(lvarRow("nazwa_ang")), "", lvarRow("nazwa_ang"))), CStr(IIf(IsDBNull(lvarRow("nazwa")), "", lvarRow("nazwa"))), CStr(IIf(IsDBNull(lvarRow("nazwa_niem")), "", lvarRow("nazwa_niem"))), CStr(IIf(IsDBNull(lvarRow("norma_wykonania")), "", lvarRow("norma_wykonania"))), CStr(IIf(IsDBNull(lvarRow("stop_id")), -1, lvarRow("stop_id")))))
            Next
        End If

    End Sub

    Private Sub PobierzWszystkieWaluty()

        Dim lvarDataTable As New DataTable

        lvarDataTable = clsWaluta.PobierzWszystkieOpcje(mvarDbHolder, VAR_ERROR_HANDLER)
        cmbWaluta.Items.Clear()
        If Not lvarDataTable Is Nothing Then
            For Each lvarRow As DataRow In lvarDataTable.Rows
                cmbWaluta.Items.Add(New clsWaluta(CStr(lvarRow("id")), CType(IIf(IsDBNull(lvarRow("nazwa")), "", lvarRow("nazwa")), String)))
            Next
        End If

    End Sub

#End Region

    Private Sub Blokowanie()

        mvarBlokowanie = New clsRecordLock(VAR_CONNECTION_PARAMETERS, mvarMaterial.Id, "oem.material", VAR_ERROR_HANDLER, CInt(mvarUzytkownik.Id))
        mvarWyznBlokowania = True

        If Not mvarBlokowanie.RecordLocked Then
            MessageBox.Show("TYLKO DO ODCZYTU", "WIREMET")
            grbDane.Enabled = False
            cmdZapisz.Enabled = False
            cmdWyczysc.Enabled = False
        End If

    End Sub

    Private Sub Zapisz()

        If txtWytop.Text.Length = 0 Or cmbGatunek.SelectedIndex = -1 Then
            MessageBox.Show("Podaj konieczne watości (wytop oraz gatunek).", "WIREMET")
            txtWytop.Focus()
            mvarCancel = True
        Else
            mvarDbHolder.BeginTrans()

            With mvarMaterial
                .Id = mvarMaterial.Id
                If cmbDostawca.SelectedIndex <> -1 Then
                    .DostawcaId = CType(cmbDostawca.SelectedItem, clsDostawca).Id
                End If
                .GatunkeId = CType(cmbGatunek.SelectedItem, clsGatunek).Id
                .Wytop = txtWytop.Text
                .DataUtworzenia = dtpDataWpisu.Value
                .Wymiar = Replace(txtWymiar.Text, ",", ".")
                .Wytrzymalosc = Replace(txtWytrzymalosc.Text, ",", ".")
                .Wydluzenie = Replace(txtWydluzenie.Text, ",", ".")
                .Ilosc = Replace(txtIlosc.Text, ",", ".")
                .Cena = Replace(txtCena.Text, ",", ".")
                If cmbWaluta.SelectedIndex <> -1 Then
                    .WalutaId = CType(cmbWaluta.SelectedItem, clsWaluta).Id
                End If
                AktualizacjaSkladuChemicznego()
                .Save(mvarDbHolder)
            End With

            mvarDbHolder.CommitTrans()
            If mvarWyznEdytowania = False Then
                WyczyscInformacje()
                mvarMaterial = New clsInhMaterial
            End If
            mvarChanged = False
            mvarCancel = False
            MessageBox.Show("Zapisano do bazy", "WIREMET")
            txtWytop.Focus()
        End If

    End Sub

    Private Sub WyczyscInformacje()

        cmbDostawca.SelectedIndex = -1
        cmbGatunek.SelectedIndex = -1
        txtWytop.Text = String.Empty
        dtpDataWpisu.Value = Today.Date
        txtWymiar.Text = "0,0"
        txtWytrzymalosc.Text = "0,0"
        txtWydluzenie.Text = "0,0"
        txtIlosc.Text = "0,0"
        txtCena.Text = "0,0"
        cmbWaluta.SelectedIndex = -1

    End Sub

    Private Sub UzupelnienieDanych()

        Dim lvarIndex As Integer

        lvarIndex = cmbDostawca.FindString(mvarMaterial.Dostawca.NazwaFirmy)
        cmbDostawca.SelectedIndex = lvarIndex

        'lvarIndex = cmbStop.FindString(mvarMaterial.Gatunek.)
        'cmbStop.SelectedIndex = lvarIndex

        lvarIndex = cmbGatunek.FindString(mvarMaterial.Gatunek.NazwaAng)
        cmbGatunek.SelectedIndex = lvarIndex

        txtWytop.Text = mvarMaterial.Wytop
        dtpDataWpisu.Value = mvarMaterial.DataUtworzenia
        txtWymiar.Text = mvarMaterial.Wymiar
        txtWytrzymalosc.Text = mvarMaterial.Wytrzymalosc
        txtWydluzenie.Text = mvarMaterial.Wydluzenie
        txtIlosc.Text = mvarMaterial.Ilosc
        txtCena.Text = mvarMaterial.Cena
        lvarIndex = cmbWaluta.FindString(mvarMaterial.Waluta.Nazwa)
        cmbWaluta.SelectedIndex = lvarIndex

    End Sub

    Private Sub SzukajGatunku(IdGatunku As Integer)
        If IdGatunku <> 0 Then
            For Each lvarTmp In cmbGatunek.Items
                If IdGatunku = CType(lvarTmp, clsGatunek).Id Then
                    cmbGatunek.SelectedItem = lvarTmp
                    WyswietlPierwiastki()
                End If
            Next
        End If
    End Sub

    Private Sub cmdZapisz_Click(sender As System.Object, e As System.EventArgs) Handles cmdZapisz.Click
        If mvarChanged Then
            Zapisz()
        Else
            MessageBox.Show("Nie wprowadzono żadnych zmian.", "WIREMET")
        End If
    End Sub

    Private Sub cmdAnuluj_Click(sender As System.Object, e As System.EventArgs) Handles cmdAnuluj.Click
        mvarChanged = False
        Close()
    End Sub

    Private Sub cmdWyczysc_Click(sender As System.Object, e As System.EventArgs) Handles cmdWyczysc.Click
        WyczyscInformacje()
    End Sub

    Private Sub frmMaterial_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        If mvarWyznBlokowania Then
            If mvarBlokowanie.RecordLocked Then
                mvarBlokowanie.Unlock()
            End If
        End If
        mvarDbHolder.Close()
    End Sub

    Private Sub frmMaterial_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        If mvarChanged Then
            Select Case MsgBox("Czy zapisać materiał do bazy?", MsgBoxStyle.YesNo, "WIREMET")
                Case MsgBoxResult.No
                Case MsgBoxResult.Yes
                    Zapisz()
                    If mvarCancel = True Then
                        e.Cancel = True
                    End If
            End Select
        End If

    End Sub

#Region "Tab"

    Private Sub cmbDostawca_KeyPress(sender As Object, e As KeyPressEventArgs) Handles cmbDostawca.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            cmbGatunek.Focus()
        End If
    End Sub

    Private Sub cmbGatunek_KeyPress(sender As Object, e As KeyPressEventArgs) Handles cmbGatunek.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            txtWytop.Focus()
        End If
    End Sub

    Private Sub txtWytop_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtWytop.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            dtpDataWpisu.Focus()
        End If
    End Sub

    Private Sub dtpDataWpisu_KeyPress(sender As Object, e As KeyPressEventArgs) Handles dtpDataWpisu.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            txtWymiar.Focus()
        End If
    End Sub

    Private Sub txtWymiar_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtWymiar.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            txtWytrzymalosc.Focus()
        End If
    End Sub

    Private Sub txtWytrzymalosc_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtWytrzymalosc.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            txtWydluzenie.Focus()
        End If
    End Sub

    Private Sub txtWydluzenie_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtWydluzenie.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            txtIlosc.Focus()
        End If
    End Sub

    Private Sub txtIlosc_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtIlosc.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            txtCena.Focus()
        End If
    End Sub

    Private Sub txtCena_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtCena.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            cmbWaluta.Focus()
        End If
    End Sub

    Private Sub cmbWaluta_KeyPress(sender As Object, e As KeyPressEventArgs) Handles cmbWaluta.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            cmdZapisz.Focus()
        End If
    End Sub

#End Region

#Region "Changed"

    Private Sub cmbDostawca_SelectedValueChanged(sender As Object, e As EventArgs) Handles cmbDostawca.SelectedValueChanged
        mvarChanged = True
    End Sub

    Private Sub cmbGatunek_SelectedValueChanged(sender As Object, e As EventArgs) Handles cmbGatunek.SelectedValueChanged
        WyczyscGRBSkladChemiczny()
        WyswietlPierwiastki()
        mvarChanged = True
    End Sub

    Private Sub txtWytop_TextChanged(sender As Object, e As EventArgs) Handles txtWytop.TextChanged
        mvarChanged = True
    End Sub


    Private Sub dtpDataWpisu_ValueChanged(sender As Object, e As EventArgs) Handles dtpDataWpisu.ValueChanged
        mvarChanged = True
    End Sub

    Private Sub txtWymiar_TextChanged(sender As Object, e As EventArgs) Handles txtWymiar.TextChanged
        mvarChanged = True
    End Sub

    Private Sub txtWytrzymalosc_TextChanged(sender As Object, e As EventArgs) Handles txtWytrzymalosc.TextChanged
        mvarChanged = True
    End Sub

    Private Sub txtWydluzenie_TextChanged(sender As Object, e As EventArgs) Handles txtWydluzenie.TextChanged
        mvarChanged = True
    End Sub

    Private Sub txtIlosc_TextChanged(sender As Object, e As EventArgs) Handles txtIlosc.TextChanged
        mvarChanged = True
    End Sub

    Private Sub txtCena_TextChanged(sender As Object, e As EventArgs) Handles txtCena.TextChanged
        mvarChanged = True
    End Sub

    Private Sub cmbWaluta_SelectedValueChanged(sender As Object, e As EventArgs) Handles cmbWaluta.SelectedValueChanged
        mvarChanged = True
    End Sub

#End Region


    Private Sub cmdGatunek_Click(sender As Object, e As EventArgs) Handles cmdGatunek.Click

        Dim lvarFrm As frmGatunekMaterialuSzukanie
        Dim lvarId As Integer

        If cmbGatunek.SelectedIndex <> -1 Then
            lvarFrm = New frmGatunekMaterialuSzukanie(CBool(1), CType(cmbGatunek.SelectedItem, clsGatunek).Id) 'frmGatunekMaterialu(CType(cmbGatunek.SelectedItem, clsGatunek).Id)
            With lvarFrm
                .Location = New Point(10, 300)
                .ShowDialog()
            End With
            lvarId = CType(cmbGatunek.SelectedItem, clsGatunek).Id
        Else
            lvarFrm = New frmGatunekMaterialuSzukanie(CBool(1), 0)
            With lvarFrm
                .Location = New Point(10, 300)
                .ShowDialog()
            End With
        End If
        PobierzWszystkieGatunkiMaterialu()
        WyczyscGRBSkladChemiczny()
        If lvarFrm.dgvInformacje.SelectedRows.Count > 0 And lvarFrm.Zmiana Then
            For lvarI As Integer = 0 To cmbGatunek.Items.Count - 1
                If CType(cmbGatunek.Items(lvarI), clsGatunek).Id = CType(lvarFrm.dgvInformacje.SelectedRows(0).Tag, clsInhGatunek).Id Then
                    cmbGatunek.SelectedIndex = lvarI
                    Exit For
                End If
            Next
        Else
            For lvarI As Integer = 0 To cmbGatunek.Items.Count - 1
                If CType(cmbGatunek.Items(lvarI), clsGatunek).Id = lvarId Then
                    cmbGatunek.SelectedIndex = lvarI
                    Exit For
                End If
            Next
        End If

    End Sub

    Private Sub cmdWaluta_Click(sender As Object, e As EventArgs) Handles cmdWaluta.Click

        Dim lvarFrm As frmWalutaSzukanie
        Dim lvarId As Integer

        If cmbWaluta.SelectedIndex <> -1 Then
            lvarFrm = New frmWalutaSzukanie(CBool(1), CInt(CType(cmbWaluta.SelectedItem, clsWaluta).Id))
            With lvarFrm
                .Location = New Point(200, 350)
                .ShowDialog()
            End With
            lvarId = CInt(CType(cmbWaluta.SelectedItem, clsWaluta).Id)
        Else
            lvarFrm = New frmWalutaSzukanie(CBool(1), 0)
            With lvarFrm
                .Location = New Point(200, 350)
                .ShowDialog()
            End With
        End If
        PobierzWszystkieWaluty()
        If lvarFrm.dgvInformacje.SelectedRows.Count > 0 And lvarFrm.Zmiana Then
            For lvarI As Integer = 0 To cmbWaluta.Items.Count - 1
                If CType(cmbWaluta.Items(lvarI), clsWaluta).Id = CType(lvarFrm.dgvInformacje.SelectedRows(0).Tag, clsInhWaluta).Id Then
                    cmbWaluta.SelectedIndex = lvarI
                    Exit For
                End If
            Next
        Else
            For lvarI As Integer = 0 To cmbWaluta.Items.Count - 1
                If CInt(CType(cmbWaluta.Items(lvarI), clsWaluta).Id) = lvarId Then
                    cmbWaluta.SelectedIndex = lvarI
                    Exit For
                End If
            Next
        End If

    End Sub

    Private Sub cmdDostawca_Click(sender As Object, e As EventArgs) Handles cmdDostawca.Click

        Dim lvarFrm As frmDostawca
        Dim lvarId As Integer


        If cmbDostawca.SelectedIndex <> -1 Then
            lvarFrm = New frmDostawca(CBool(1), CInt(CType(cmbDostawca.SelectedItem, clsDostawca).Id))
            With lvarFrm
                .Location = New Point(200, 300)
                .ShowDialog()
            End With
            lvarId = CInt(CType(cmbDostawca.SelectedItem, clsDostawca).Id)
        Else
            lvarFrm = New frmDostawca(CBool(1), 0)
            With lvarFrm
                .Location = New Point(200, 300)
                .ShowDialog()
            End With

        End If
        PobierzWszystkichDostawcow()
        If lvarFrm.dgvInformacje.SelectedRows.Count > 0 And lvarFrm.Zmiana Then
            For lvarI As Integer = 0 To cmbDostawca.Items.Count - 1
                If CType(cmbDostawca.Items(lvarI), clsDostawca).Id = CType(lvarFrm.dgvInformacje.SelectedRows(0).Tag, clsInhDostawca).Id Then
                    cmbDostawca.SelectedIndex = lvarI
                    Exit For
                End If
            Next
        Else
            For lvarI As Integer = 0 To cmbDostawca.Items.Count - 1
                If CInt(CType(cmbDostawca.Items(lvarI), clsDostawca).Id) = lvarId Then
                    cmbDostawca.SelectedIndex = lvarI
                    Exit For
                End If
            Next
        End If

    End Sub


    Private Sub WyswietlPierwiastki()

        Dim lvarStop As clsInhStop
        Dim lvarXTxt As Integer = 50
        Dim lvarXLbl As Integer = 10
        Dim lvarYTxt As Integer = 0
        Dim lvarYLbl As Integer = 0
        Dim lvarLicznik As Integer = 0
        Dim lvarLicznikKolumn As Integer = 1
        Dim lvarWidth As Integer = 0

        If cmbGatunek.SelectedIndex <> -1 Then

            lvarStop = New clsInhStop(CInt(CType(cmbGatunek.SelectedItem, clsGatunek).StopId), mvarDbHolder)

            For Each lvarSklad As clsInhSklad In lvarStop.Sklad
                Dim lvarLbl As New Label()
                lvarLbl.Location = New Point(lvarXLbl, 30 + lvarYLbl)
                lvarLbl.Size = CType(New Point(36, 13), Drawing.Size)
                lvarLbl.TextAlign = ContentAlignment.BottomRight
                lvarLbl.Text = lvarSklad.Pierwiastek.Nazwa
                grbSkladChemiczny.Controls.Add(lvarLbl)

                Dim lvarTxt As New TextBox()
                lvarTxt.Location = New Point(lvarXTxt, 27 + lvarYTxt)
                lvarTxt.Size = CType(New Point(44, 22), Drawing.Size)
                lvarTxt.Tag = lvarSklad
                mvarTextList.Add(lvarTxt)
                grbSkladChemiczny.Controls.Add(lvarTxt)

                lvarLicznik = lvarLicznik + 1
                If lvarLicznik = 9 And lvarStop.Sklad.Count > 9 * lvarLicznikKolumn Then
                    lvarXTxt = lvarXTxt + 85
                    lvarXLbl = lvarXLbl + 85
                    lvarYTxt = 0
                    lvarYLbl = 0
                    lvarLicznik = 0
                    lvarWidth = lvarWidth + 80
                    Me.Width = CInt(Me.Width + lvarWidth)
                    grbDane.Width = CInt(grbDane.Width + lvarWidth)
                    grbSkladChemiczny.Width = CInt(grbSkladChemiczny.Width + lvarWidth)
                    lvarLicznikKolumn = lvarLicznikKolumn + 1
                Else
                    lvarYTxt = lvarYTxt + 25
                    lvarYLbl = lvarYLbl + 25
                End If
            Next

            If mvarMaterial.Id <> -1 Then
                For Each lvarSkladChemiczny As clsInhSkladChemiczny In mvarMaterial.SkladChemiczny
                    For Each lvarTxt As TextBox In mvarTextList
                        If CType(lvarTxt.Tag, clsSklad).Id = lvarSkladChemiczny.Sklad.Id Then
                            lvarTxt.Text = lvarSkladChemiczny.Wartosc
                        End If
                    Next
                Next
            End If

        End If

    End Sub

    Private Sub AktualizacjaSkladuChemicznego()

        For Each lvarSkladChemiczny As clsInhSkladChemiczny In mvarMaterial.SkladChemiczny
            mvarMaterial.SkladChemicznyDel.Add(lvarSkladChemiczny)
        Next

        For Each lvarCheck As TextBox In mvarTextList
            If lvarCheck.Text <> String.Empty Then
                Dim lvarSkladChemiczny As New clsInhSkladChemiczny
                With lvarSkladChemiczny
                    .MaterialId = mvarMaterial.Id
                    .Wartosc = lvarCheck.Text
                    .SkladId = CType(lvarCheck.Tag, clsSklad).Id
                End With
                mvarMaterial.SkladChemiczny.Add(lvarSkladChemiczny)
            End If
        Next

    End Sub

    Private Sub WyczyscGRBSkladChemiczny()

        If mvarTextList.Count <> 0 Then
            grbSkladChemiczny.Controls.Clear()
            mvarTextList.Clear()
            Me.Width = 555
            grbDane.Width = 518
            grbSkladChemiczny.Width = 124
        End If

    End Sub

End Class